package com.byt.framlib.basemap;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;

/**
 * 类名称：NETLocationManager
 * 类描述：高德地图定位管理器
 */
public class NETLocationManager implements AMapLocationListener {

    private Context context;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption = null;
    private NETLocationListener mNETLocationListener;

    /**
     * 方法名称：NETLocationManager
     * 方法描述：高德位置管理器
     *
     * @param context              视图
     * @param mNETLocationListener 回调监听器
     */
    public NETLocationManager(Context context, NETLocationListener mNETLocationListener) {
        this.context = context;
        this.mNETLocationListener = mNETLocationListener;
        //初始化定位
        mLocationClient = getLocationClient();
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(getDefaultinitLocationOption());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
    }

    /**
     * 方法名称：getLocationClient
     * 方法描述：获取AMapLocationClient的实例化对象
     */
    public AMapLocationClient getLocationClient() {
        if (mLocationClient != null)
            return this.mLocationClient;
        return mLocationClient = new AMapLocationClient(context.getApplicationContext());
    }

    /**
     * 方法名称：start
     * 方法描述：开启高德地图定位模式
     */
    public void start() {
        //启动定位
        if (mLocationClient != null) {
            mLocationClient.startLocation();
        }
    }

    /**
     * 方法名称：getDefaultinitLocationOption
     * 方法描述：返回设置AMapLocationClientOption的结果对象
     */
    public AMapLocationClientOption getDefaultinitLocationOption() {
        if (mLocationOption == null) {
            //初始化AMapLocationClientOption对象
            mLocationOption = new AMapLocationClientOption();
            //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
            mLocationOption.setLocationMode(AMapLocationMode.Battery_Saving);
            //获取一次定位结果：
            //该方法默认为false。
            mLocationOption.setOnceLocation(true);
            //获取最近3s内精度最高的一次定位结果：
            mLocationOption.setOnceLocationLatest(true);
            //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
//            mLocationOption.setInterval(2000);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否允许模拟位置, 默认为false，不允许模拟位置
            mLocationOption.setMockEnable(false);
            //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
            mLocationOption.setHttpTimeOut(10000);
            //关闭缓存机制
            mLocationOption.setLocationCacheEnable(false);
        }
        return mLocationOption;
    }

    /**
     * 方法名称：onStop
     * 方法描述：停止高德位置监听器
     * 创建人: 钟文浩
     * 创建时间：2017/4/28 13:38
     */
    public void onStop() {
        //停止定位后，本地定位服务并不会被销毁
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
        }
    }

    /**
     * 方法名称：onDestroy
     * 方法描述：销毁高德位置管理器
     */
    public void onDestroy() {
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
        }
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        /**
         * Log.e("aMapLocation", aMapLocation.getAddress() + "---地址");
         * Log.e("aMapLocation", aMapLocation.getLongitude() + " ---经度");
         * Log.e("aMapLocation", aMapLocation.getTime() + " ---时间");
         * Log.e("aMapLocation", aMapLocation.getLocationType() + " ---位置类型");
         * Log.e("aMapLocation", aMapLocation.getLatitude() + " ---纬度");
         * Log.e("aMapLocation", aMapLocation.getAccuracy() + " ---精度信息");
         * Log.e("aMapLocation", aMapLocation.getCountry() + " ---国家信息");
         * Log.e("aMapLocation", aMapLocation.getProvince() + " ---省信息");
         * Log.e("aMapLocation", aMapLocation.getCity() + " ---城市信息");
         * Log.e("aMapLocation", aMapLocation.getDistrict() + " ---城区信息");
         * Log.e("aMapLocation", aMapLocation.getStreet() + " ---街道信息");
         * Log.e("aMapLocation", aMapLocation.getStreetNum() + " ---街道门牌号信息");
         * Log.e("aMapLocation", aMapLocation.getCityCode() + " ---城市编码");
         * Log.e("aMapLocation", aMapLocation.getAdCode() + " ---地区编码");
         * Log.e("aMapLocation", aMapLocation.getAoiName() + " ---定位点的AOI信息");
         * Log.e("aMapLocation", aMapLocation.getBuildingId() + " ---室内定位的建筑物Id");
         * Log.e("aMapLocation", aMapLocation.getFloor() + " ---室内定位的楼层");
         * Log.e("aMapLocation", aMapLocation.getGpsAccuracyStatus() + " ---获取GPS的当前状态");
         * Log.e("aMapLocation", aMapLocation.getSpeed() + " ---获取速度");
         * Log.e("aMapLocation", aMapLocation.getAltitude() + " ---海拔高度");
         * Log.e("aMapLocation", aMapLocation.getPoiName() + " ---getPoiName");
         * Log.e("aMapLocation", aMapLocation.getProvider() + " ---getProvider");
         * Log.e("aMapLocation", aMapLocation.getProvince() + " ---getProvince");
         * Log.e("aMapLocation", aMapLocation.getErrorCode() + " ---getErrorCode");
         * Log.e("aMapLocation", aMapLocation.getBearing() + " ---航向角");
         * Log.e("aMapLocation", aMapLocation.getRoad() + " ---路");
         */
        if (mNETLocationListener != null) {
            mNETLocationListener.onLocationChanged(aMapLocation);
        }
    }
}